package com.dhu.service.impl;

import com.dhu.dao.BarMapper;
import com.dhu.dao.UserBarFollowMapper;
import com.dhu.dao.UserMapper;
import com.dhu.pojo.UserBarFollow;
import com.dhu.service.UserBarFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserBarFollowServiceImpl implements UserBarFollowService  {
    @Autowired
    UserBarFollowMapper userBarFollowMapper;
    @Autowired
    BarMapper barMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public UserBarFollow ifFollowBar(int userId, int barId) {
        try {
            return userBarFollowMapper.ifFollowBar(userId,barId);
        } catch (Exception e) {
            return null;
        }
    }

    // 改变关注状态
    @Override
    public int changeFollow(boolean isFollowBar, int userId, int barId) {
        try {
            if (isFollowBar) {
                // 关注数-1
                barMapper.delFollowNum(barId);
                // 用户关注的吧-1
                userMapper.subBarNum(userId);
                return userBarFollowMapper.disFollow(userId, barId);
            } else {
                // 关注数+1
                barMapper.addFollowNum(barId);
                // 用户关注的吧+1
                userMapper.addBarNum(userId);
                return userBarFollowMapper.follow(userId, barId);
            }
        } catch (Exception e) {
            return -1;
        }
    }
}
